package com.gexin.common.job;

import com.gexin.core.web.SystemBaseController;
import com.gexin.model.activity.MemberInfo;
//import com.gexin.model.activity.base.BaseInstmtQuartz;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.log.Log;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 演示定时器
 * @author Administrator
 */
//@ControllerBind(controllerKey = "/system/TestJob", viewPath = "WEB-INF/system/advertising")
@ControllerBind(controllerKey = "/asd")
public class TestJob implements Runnable{
	//任务组名
	private static final String JOB_GROUP_NAME = "my_group";
	//触发器组名
	private static final String TRIGGER_GROUP_NAME = "my_trigger";
	//调度工厂
	private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();

	static Log log = Log.getLog(ActivityJob.class);

		public void run() {
			List<MemberInfo> memberInfos = (List<MemberInfo>) MemberInfo.dao.findFirst("select * from t_member_info");
			try {
				TestJob.addJob("test",TestJob.class,"*/5 * * * * ?");
				System.out.println(memberInfos);
				log.info("987987");
				memberInfos.forEach(memberInfo -> {
//					memberInfo.setIntegral(0);
					memberInfo.update();
				});
				if(memberInfos!=null){
					log.info("12312312312312");
				}
			} catch (SchedulerException e) {
				e.printStackTrace();
			}
		}


	/**
	 * @Description: 添加一个定时任务
	 *
	 * @Title: QuartzManager.java
	 */
	public static void addJob(String jobName ,Class jobClazz ,String cron) throws SchedulerException {
		//try {
		//JobDetail jobDetail = new JobDetailImpl(jobName, jobGroupName, jobClass);// 任务名，任务组，任务执行类
		// 触发器
		//CronTriggerImpl trigger = new CronTriggerImpl(triggerName, triggerGroupName);// 触发器名,触发器组
		//time=("*/2");
			/*trigger.setCronExpression(time);// 触发器时间设定
			sched.scheduleJob(jobDetail, trigger);*/
		/*} catch (Exception e) {
			throw new RuntimeException(e);
		}
		*/
		// 1、创建调度器Scheduler
		/*SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();
		// 2、创建JobDetail实例，并与PrintWordsJob类绑定(Job执行内容)
		JobDetail jobDetail = JobBuilder.newJob(PrintWordsJob.class)
				.usingJobData("jobDetail1", "这个Job用来测试的")
				.withIdentity("job1", "group1").build();
		// 3、构建Trigger实例,每隔1s执行一次
		Date startDate = new Date();
		startDate.setTime(startDate.getTime() + 5000);

		Date endDate = new Date();
		endDate.setTime(startDate.getTime() + 5000);

		CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1")
				.usingJobData("trigger1", "这是jobDetail1的trigger")
				.startNow()//立即生效
				.startAt(startDate)
				.endAt(endDate)
				.withSchedule(CronScheduleBuilder.cronSchedule("* 30 10 ? * 1/5 2018"))
				.build();

		//4、执行
		scheduler.scheduleJob(jobDetail, cronTrigger);
		System.out.println("--------scheduler start ! ------------");
		scheduler.start();
		System.out.println("--------scheduler shutdown ! ------------");*/


		try {
			//获取Scheduler
			Scheduler scheduler = schedulerFactory.getScheduler();
			// 任务名，任务组，任务执行类
			JobDetail jobDetail = JobBuilder.newJob(jobClazz).withIdentity(jobName, JOB_GROUP_NAME).build();
			// 传参 可以不要
			jobDetail.getJobDataMap().put("repId", jobName);
			// 触发器
			TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
			// 触发器名,触发器组
			triggerBuilder.withIdentity(jobName, TRIGGER_GROUP_NAME);
			triggerBuilder.startNow();
			// 触发器时间设定
			triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
			// 创建Trigger对象
			CronTrigger trigger = (CronTrigger) triggerBuilder.build();
			// 调度容器设置JobDetail和Trigger
			scheduler.scheduleJob(jobDetail, trigger);
			// 启动
			if (!scheduler.isShutdown()) {
				scheduler.start();
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
}
